# TF expression analysis
# 2024-11-6

library(tidyverse)
library(readxl)
library(ggstatsplot)

prefilename <- "D:/Work/TF/Ph1/" # for working computer
# prefilename <- "." # for testing

data_file <- "/TF_Analysis.xlsx"


tumor2en <- function(tumor_cn) {
  # Translate the Chinese tumor into English
  # Args:
  #   tumor_cn: a list of tumor of Chinese
  # Returns:
  #   a list of tumor of English

  # Create a mapping dictionary
  tumor_map <- c(
    "宫颈癌" = "CC",
    "胰腺癌" = "PDAC",
    "尿路上皮癌" = "UC",
    "卵巢癌" = "OC",
    "鼻咽癌" = "NPC",
    "头颈鳞癌" = "HNSCC",
    "输卵管癌" = "OC", # 输卵管癌 视为 OC
    "前列腺癌" = "Prostate"
  )

  # Use str_replace_all() function to replace all Chinese tumors with English
  tumor_en <- str_replace_all(tumor_cn, tumor_map)

  return(tumor_en)
}

data_file <- paste0(prefilename, data_file)

patients <- read_xlsx(data_file,
  sheet = "TF_expression",
  range = cell_cols("A:I")
)

glimpse(patients)  


patients$TF <- as.numeric(patients$TF)

patients <- patients |> 
  mutate(Tumor = tumor2en(Tumor)) |> 
  filter(!is.na(TF))


tf_boxplot <- ggplot(patients, aes(reorder(Tumor, -TF, median), TF,
                                   color = Tumor)) +
  geom_boxplot() +
  # geom_violin() +
  geom_jitter(width = 0.2, height = 0.1, alpha=0.7) +
  labs(x = "", y = "H score")

tf_boxplot


ggbetweenstats(data = patients,
               x = Tumor,
               y = TF,
               caption = "cap",
               title = "tile")

ggsave("TF_boxplot.png", plot=tf_boxplot, width = 16, height = 7, dpi = "print")
